{% extends 'base.html' %}

{% block title %}我的图书{% endblock %}

{% block content %}
<div class="row">
    <!-- 用户统计信息 -->
    <div class="col-12 mb-4">
        <div class="row">
            <div class="col-md-3">
                <div class="stats-card text-center">
                    <h3>{{ stats.total_borrowed }}</h3>
                    <p>总借阅数</p>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stats-card text-center">
                    <h3>{{ stats.total_returned }}</h3>
                    <p>已归还</p>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stats-card text-center">
                    <h3>{{ borrow_records.count }}</h3>
                    <p>当前借阅</p>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stats-card text-center">
                    <h3>{{ stats.overdue_count }}</h3>
                    <p>逾期未还</p>
                </div>
            </div>
        </div>
    </div>

    <!-- 用户档案信息 -->
    <div class="col-12 mb-4">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-user me-2"></i>我的档案</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-2 text-center">
                        {% if profile.avatar %}
                            <img src="{{ profile.avatar.url }}" class="rounded-circle" style="width: 80px; height: 80px; object-fit: cover;">
                        {% else %}
                            <div class="bg-light rounded-circle d-flex align-items-center justify-content-center mx-auto" style="width: 80px; height: 80px;">
                                <i class="fas fa-user fa-2x text-muted"></i>
                            </div>
                        {% endif %}
                    </div>
                    <div class="col-md-10">
                        <div class="row">
                            <div class="col-md-6">
                                <p><strong>用户名:</strong> {{ user.username }}</p>
                                <p><strong>最大借阅数量:</strong> {{ profile.max_borrow_limit }}本</p>
                                <p><strong>当前借阅:</strong> {{ profile.get_current_borrowed_count }}本</p>
                            </div>
                            <div class="col-md-6">
                                {% if profile.student_id %}
                                <p><strong>学号/工号:</strong> {{ profile.student_id }}</p>
                                {% endif %}
                                {% if profile.department %}
                                <p><strong>院系/部门:</strong> {{ profile.department }}</p>
                                {% endif %}
                                {% if profile.phone %}
                                <p><strong>手机号:</strong> {{ profile.phone }}</p>
                                {% endif %}
                            </div>
                        </div>
                        <div class="mt-3">
                            <a href="{% url 'user_profile' %}" class="btn btn-primary">
                                <i class="fas fa-edit me-1"></i>编辑档案
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 当前借阅列表 -->
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-bookmark me-2"></i>当前借阅</h5>
            </div>
            <div class="card-body">
                {% if borrow_records %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>图书信息</th>
                                <th>借阅日期</th>
                                <th>应还日期</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in borrow_records %}
                            <tr>
                                <td>
                                    <div class="d-flex align-items-center">
                                        {% if record.book.cover_image %}
                                            <img src="{{ record.book.cover_image.url }}" class="rounded me-3" style="width: 50px; height: 60px; object-fit: cover;">
                                        {% else %}
                                            <div class="bg-light rounded me-3 d-flex align-items-center justify-content-center" style="width: 50px; height: 60px;">
                                                <i class="fas fa-book text-muted"></i>
                                            </div>
                                        {% endif %}
                                        <div>
                                            <h6 class="mb-1">{{ record.book.title }}</h6>
                                            <p class="text-muted small mb-0">{{ record.book.author.name }}</p>
                                        </div>
                                    </div>
                                </td>
                                <td>{{ record.borrow_date|date:"Y-m-d" }}</td>
                                <td>
                                    <span class="{% if record.is_overdue %}text-danger{% elif record.due_date|date:'Y-m-d' == today %}text-warning{% endif %}">
                                        {{ record.due_date|date:"Y-m-d" }}
                                    </span>
                                </td>
                                <td>
                                    {% if record.is_overdue %}
                                        <span class="badge bg-danger">
                                            <i class="fas fa-exclamation-triangle me-1"></i>逾期 {{ record.get_days_overdue }}天
                                        </span>
                                    {% elif record.due_date|date:'Y-m-d' == today %}
                                        <span class="badge bg-warning">
                                            <i class="fas fa-clock me-1"></i>今日到期
                                        </span>
                                    {% else %}
                                        <span class="badge bg-success">
                                            <i class="fas fa-check me-1"></i>正常
                                        </span>
                                    {% endif %}
                                </td>
                                <td>
                                    <div class="btn-group" role="group">
                                        <a href="{% url 'book_detail' record.book.pk %}" class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        {% if record.renewals_left > 0 %}
                                        <a href="{% url 'renew_loan' record.pk %}" class="btn btn-sm btn-warning">
                                            <i class="fas fa-redo me-1"></i>续借
                                        </a>
                                        {% endif %}
                                        <a href="{% url 'return_book' record.pk %}" class="btn btn-sm btn-success">
                                            <i class="fas fa-undo me-1"></i>归还
                                        </a>
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% else %}
                <div class="text-center py-5">
                    <i class="fas fa-book-open fa-3x text-muted mb-3"></i>
                    <h5 class="text-muted">您还没有借阅任何图书</h5>
                    <p class="text-muted">快去图书馆找找您喜欢的书吧！</p>
                    <a href="{% url 'user_book_list' %}" class="btn btn-primary">
                        <i class="fas fa-search me-1"></i>浏览图书
                    </a>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- 逾期提醒模态框 -->
{% if stats.overdue_count > 0 %}
<div class="modal fade" id="overdueModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title">
                    <i class="fas fa-exclamation-triangle me-2"></i>逾期提醒
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>您有 <strong>{{ stats.overdue_count }}</strong> 本图书已逾期，请尽快归还！</p>
                <p class="text-muted small">逾期图书可能会影响您的借阅权限。</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">我知道了</button>
            </div>
        </div>
    </div>
</div>
{% endif %}
{% endblock %}

{% block extra_js %}
<script>
// 如果有逾期图书，自动显示提醒
{% if stats.overdue_count > 0 %}
document.addEventListener('DOMContentLoaded', function() {
    var overdueModal = new bootstrap.Modal(document.getElementById('overdueModal'));
    overdueModal.show();
});
{% endif %}
</script>
{% endblock %}